//
//  ApproveDisagreeViewController.m
//  CloudMD
//
//  Created by centling on 16/3/25.
//  Copyright © 2016年 HuaZhengInfo. All rights reserved.
//

#import "ApproveDisagreeViewController.h"
#import "DeclarationDetailApproveModel.h"

@interface ApproveDisagreeViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate> {
    UIImageView * _view1;
    UIImageView * _view2;
    UILabel *_roomLabel;
    UIView * _mainView;
    UITableView *_roomTableView;  //退回岗位列表
    XYTextView * _textView; //审核意见
    NSString * _state; // 状态
    NSString * _ID; // ID
    NSString *_backGWCode; //退回岗位编码
    NSInteger _backStreamID; //退回审批流id
}

@end

@implementation ApproveDisagreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"退回审核";
    UIButton * leftButton = [GDFactory creatUIButtonWithFrame:CGRectMake(0, 0, 10, 15) backImage:@"left_arrow" title:nil titleLableColcor:nil titleLabelFont:nil];
    [leftButton addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self buildMainUI];
}

#pragma mark -- 创建UI
- (void)buildMainUI{
    
    _mainView = [GDFactory creatUIViewWithFrame:CGRectMake(0, 0, SCR_W, SCR_H) backColor:RGBA(235, 235, 235, 1.0)];
    [self.view addSubview:_mainView];
    
    
        UILabel * Label1 = [GDFactory creatUILabelWithFrame:CGRectMake(10, 15, 100, 30) text:@"退回岗位:" textColor:RED textFont:[UIFont systemFontOfSize:16]];

        [_mainView addSubview:Label1];
    
        _view1 = [GDFactory creatUIImageViewWithFrame:CGRectMake(10, CGRectGetMaxY(Label1.frame), SCR_W - 20, SCR_H/13) backgroundImage:@"下拉框_03"];

        [_mainView addSubview:_view1];
    
        //退回岗位
        _roomLabel = [GDFactory creatUILabelWithFrame:CGRectMake(10, 0, _view1.frame.size.width, _view1.frame.size.height) text:@"请选择" textColor:RGBA(164, 164, 164, 1.0) textFont:[UIFont systemFontOfSize:18]];
        _roomLabel.textAlignment = NSTextAlignmentLeft;
    
        // 添加点击手势
        UITapGestureRecognizer * roomGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(roomGseture)];
        _view1.userInteractionEnabled = YES;
        _roomLabel.userInteractionEnabled = YES;
        [_roomLabel addGestureRecognizer:roomGesture];
    
        [_view1 addSubview:_roomLabel];
    //
    //    UILabel * label2 = [GDFactory creatUILabelWithFrame:CGRectMake(10, CGRectGetMaxY(_view1.frame), 100, 30) text:@"订单类型:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
    //    if (iPhone4) {
    //        label2.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame)+5, 100, 10);
    //    }else{
    //        label2.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame), 100, 30);
    //
    //    }
    //    [_mainView addSubview:label2];
    //
    //
    //    _view2 = [GDFactory creatUIImageViewWithFrame:CGRectMake(10, CGRectGetMaxY(label2.frame), SCR_W - 20, SCR_H/12) backgroundImage:@"下拉框_03"];
    //    if (iPhone4) {
    //        _view2.frame = CGRectMake(10, CGRectGetMaxY(label2.frame) + 5, SCR_W - 20, SCR_H/13);
    //    }else{
    //
    //        _view2.frame = CGRectMake(10, CGRectGetMaxY(label2.frame), SCR_W - 20, SCR_H/13);
    //    }
    //    //    [_view2.layer setBorderWidth:1.0];
    //    //    [_view2.layer setBorderColor:RGBA(202, 202, 202, 1.0).CGColor];
    //    _view2.userInteractionEnabled = YES;
    //    [_mainView addSubview:_view2];
    //
    //    // 订单类型
    //
    //    _cateButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, _view2.frame.size.width, _view2.frame.size.height)];
    //    [_cateButton setTitle:@"请选择" forState:UIControlStateNormal];
    //    [_cateButton setTitleColor:RGBA(164, 164, 164, 1.0) forState:UIControlStateNormal];
    //    _cateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //    _cateButton.titleLabel.font = [UIFont systemFontOfSize:18];
    //
    //    [_cateButton addTarget:self action:@selector(cateGesture) forControlEvents:UIControlEventTouchUpInside];
    //    [_view2 addSubview:_cateButton];
    //
    //    //    _cateLabel = [GDFactory creatUILabelWithFrame:CGRectMake(10, 0, view2.frame.size.width, view2.frame.size.height) text:@"秩序类订单" textColor:RGBA(164, 164, 164, 1.0) textFont:[UIFont systemFontOfSize:18]];
    //    ////    _cateLabel.backgroundColor = [UI/Color redColor];
    //    //    _cateLabel.textAlignment = NSTextAlignmentLeft;
    //    //    // 添加点击手势
    //    //    UITapGestureRecognizer * cateGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cateGesture)];
    //    //    _descLabel.userInteractionEnabled = YES;
    //    //    [_descLabel addGestureRecognizer:cateGesture];
    //    //
    //    //
    //    //    [view2 addSubview:_cateLabel];
    
    UILabel * label3 = [GDFactory creatUILabelWithFrame:CGRectMake(10, CGRectGetMaxY(_view1.frame)+5, 100, 30) text:@"审核意见:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
    [_mainView addSubview:label3];
    
    // 审核意见
    _textView = [[XYTextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(label3.frame)+5, SCR_W - 20, SCR_H/8)];
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.font = [UIFont systemFontOfSize:16.0];
    _textView.textColor = [UIColor blackColor];
    _textView.textAlignment = NSTextAlignmentLeft;
    //自定义键盘上面完成toolbar
    UIToolbar *customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCR_W,40}];
    customAccessoryView.barTintColor = [UIColor whiteColor];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame = CGRectMake(0, 0, 40, 40);
    [sendButton setTitle:@"完成" forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *finish = [[UIBarButtonItem alloc] initWithCustomView:sendButton];
    [customAccessoryView setItems:@[space,space,finish]];
    _textView.inputAccessoryView = customAccessoryView;
    
    [_mainView addSubview:_textView];
    
    
    //  不同意按钮
    UIButton * grabBtn = [[UIButton alloc] init];
    if (iPhone4) {
        grabBtn.frame = CGRectMake(0, 0, SCR_W/2, 30);
        grabBtn.center = CGPointMake(SCR_W * 0.5, CGRectGetMaxY(_textView.frame)+ 50);
    }else{
        
        grabBtn.frame = CGRectMake(0, 0, SCR_W/2, 40);
        grabBtn.center = CGPointMake(SCR_W * 0.5, CGRectGetMaxY(_textView.frame)+ 60);
    }
    
    [grabBtn setBackgroundImage:[UIImage imageNamed:@"3在线下单"] forState:UIControlStateNormal];
    [grabBtn setTitle:@"退回审核" forState:UIControlStateNormal];
    [grabBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    grabBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [grabBtn addTarget:self action:@selector(disagreeBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [_mainView addSubview:grabBtn];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.backArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    DeclarationDetailApproveModel *roomModel = [[DeclarationDetailApproveModel alloc] initWithDict:self.backArray[indexPath.row]];
    cell.textLabel.text = roomModel.GWNAME;
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_roomTableView removeFromSuperview];
    _roomTableView = nil;
    
    DeclarationDetailApproveModel *roomModel = [[DeclarationDetailApproveModel alloc] initWithDict:self.backArray[indexPath.row]];
    _roomLabel.text = roomModel.GWNAME;
    
    _backGWCode = roomModel.GWCODE;
    _backStreamID = [roomModel.ID integerValue];
}


// 键盘完成按钮
- (void)doneClick {
    [_textView resignFirstResponder];
}

- (void)disagreeBtnClick {
    if ([_roomLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD HUDShowMessage:@"请选择退回岗位" addedToView:self.view];
        return;
    }
    NSString *remark = _textView.text;
    if ([remark isEqualToString:@""]) {
        [MBProgressHUD HUDShowMessage:@"请填写审核意见" addedToView:self.view];
        return;
    }
    [self alertError:@"确认退回审核？"];
}

#pragma mark - 弹框提示
- (void)alertError:(NSString *)error{
    
    // 1.弹框
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:error delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        [self disagreeAction];
    }
}

- (void)disagreeAction {
    NSArray * keyArray = @[@"GUID",@"USERID",@"USERCODE",@"ISAGREE",@"APPROVEID",@"CONTENT",@"FHGWCODE",@"THAPPROVEID"];
    NSArray * valueArray = @[_GUID,USER_ID,USER_CODE,@0,@"",_textView.text,_backGWCode,@(_backStreamID)];
    NSDictionary * dict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,DeclarationApproveSubmit_URL];
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        for (NSDictionary * dataDict in array) {
            _state = [dataDict objectForKey:@"STATUS"];
            _ID = [dataDict objectForKey:@"ID"];
        }
        
        if ([_state isEqualToString:@"SUCCESS"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [MBProgressHUD HUDShowMessage:@"审核失败" addedToView:self.view];
        }
    } withTag:101];
}


#pragma mark -- 点击退回岗位
- (void)roomGseture{
    [self buildBackGWTableView];
}

- (void)buildBackGWTableView {
    if (!_roomTableView) {
        _roomTableView = [[UITableView alloc] init];
        _roomTableView.rowHeight = 30;
        //        _roomTableView.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame), _view1.frame.size.width, SCR_H - CGRectGetMaxY(_view1.frame));
        _roomTableView.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame), _view1.frame.size.width, 150);
        _roomTableView.delegate = self;
        _roomTableView.dataSource = self;
        _roomTableView.tableFooterView = [[UIView alloc] init];
        //    _roomTableView.backgroundColor = [UIColor redColor];
        [_mainView addSubview:_roomTableView];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [_roomTableView removeFromSuperview];
    _roomTableView = nil;
    [_textView resignFirstResponder];
}


- (void)clickLeft {
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
